package com.appkubes.mall.admin.application.user.user.dao;

import com.appkubes.mall.admin.application.user.user.entity.model.GetListResult;
import com.appkubes.mall.core.entity.MallAdmin;
import com.appkubes.mall.core.entity.MallUser;
import com.appkubes.mall.core.pojo.MallUserMapper;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserDao extends MallUserMapper {

    @Delete({"<script>",
            "Delete from mall_user where user_key in ",
            "<foreach item='id' index='index' collection='ids' open='(' separator=',' close=')'> #{id} </foreach> " ,
            "</script>"})
    int delete(@Param("ids") String[] ids);

    @Select({"<script>",
            "SELECT count(`user_key`) as total FROM mall_user WHERE ${queryStr}" ,
            "</script>"})
    String countLog(@Param("queryStr") String queryStr);

    @Select({"<script>",
            "SELECT " +
            "<choose><when test='isTotal!=true'>" +
            " u.*,ug.groupname as groupName,ul.levelname as levelName,count(o.amount) as orderNum,sum(o.amount) as orderAmount,sum(c.num) as carNum  " +
            "</when>" +
            "<otherwise>" +
            " count(u.user_key) as total" +
            "</otherwise></choose>" +
            " from mall_user as u " +
            "left join mall_usergroup as ug on ug.usergroup_key=u.group_key " +
            "left join mall_userlevel as ul on ul.userlevel_key=u.level_key " +
            "left join mall_order as o on u.user_key=o.user_key and o.pay_state=1 " +
            "left join mall_cart c on u.user_key=c.user_key where u.name like '%${name}%' " +
            "<when test='appKey!=null'>" +
            "and u.bind_uuid=#{appKey} " +
            "</when>" +
            "<when test='isTotal!=true'>" +
            "  group by u.user_key order by u.user_key desc LIMIT #{page},#{limit}" +
            "</when>",
            "</script>"})
    List<GetListResult> getListData(@Param("appKey") String appKey,
                                    @Param("name") String name,
                                    @Param("page") int page,
                                    @Param("limit") int limit,
                                    @Param("isTotal") boolean isTotal);

    @Select({"<script>",
            "SELECT count(user_key) as total FROM mall_user WHERE app_key=#{appKey} AND user_key NOT IN ",
            "<foreach item='id' index='index' collection='ids' open='(' separator=',' close=')'> #{id} </foreach> " ,
            "</script>"})
    String countUnBuyUser(@Param("ids") List<String> ids, @Param("appKey") String appKey);

    @Select({"SELECT extend.client_id FROM mall_user user LEFT JOIN mall_user_extend extend ON extend.user_key=user.user_key WHERE user.tel=#{tel}"})
    String queryCidByTel(@Param("tel") String tel);

    @Select({"select * from mall_user"})
    List<MallUser> ddda(MallAdmin mallAdmin);
}
